package com.kiragame.xchat;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.actionbarsherlock.ActionBarSherlock;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.kiragame.xchat.adapter.ChatRoomAdapter;
import com.kiragame.xchat.database.MessagesDataManager;
import com.kiragame.xchat.object.MessageContent;
import com.kiragame.xchat.object.MessageContent.MsgType;
import com.kiragame.xchat.service.ChatService;
import com.kiragame.xchat.utils.DataManager;
import com.kiragame.xchat.utils.KiraGameLog;
import com.quentindommerc.superlistview.SuperListview;

public class ChatActivity extends SherlockActivity {
	private SuperListview lvChatPrivate;
	private LinearLayout llSendMessage;
	private EditText etMessage;
	private Button btSend;

	private List<MessageContent> messageContentData = new ArrayList<MessageContent>();
	private ChatRoomAdapter chatAdapter;
	public static final String TO_USER = "TO_USER";
	private String toUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat);
		findViewIds();
		init();
		setEvent();
	}

	private void setEvent() {
		btSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (toUser == null) {
					return;
				}
				String message = etMessage.getText().toString();
				ChatService.getInstance().sendMessage(Integer.parseInt(toUser),
						message, MsgType.TEXT);
			}
		});
	}

	private void init() {
		toUser = getIntent().getStringExtra(TO_USER);
		KiraGameLog.d("Chat to user " + toUser);
		DataManager.getInstance(this).setCurrentChatUserId(toUser);
		chatAdapter = new ChatRoomAdapter(this, messageContentData);
		lvChatPrivate.setAdapter(chatAdapter);

		loadMessageFromDataBase();

		IntentFilter filter = new IntentFilter(
				ChatService.BROADCAST_NAME_CHAT_PRIVATE);
		LocalBroadcastManager.getInstance(this).registerReceiver(
				changeUIReceiver, filter);
	}

	private void loadMessageFromDataBase() {
		messageContentData.addAll(MessagesDataManager.getInstance(this)
				.getMessages(toUser, 0, 10));
		chatAdapter.notifyDataSetChanged();
	}

	private void findViewIds() {
		lvChatPrivate = (SuperListview) findViewById(R.id.lv_chat_private);
		llSendMessage = (LinearLayout) findViewById(R.id.ll_send_message);
		etMessage = (EditText) findViewById(R.id.et_message);
		btSend = (Button) findViewById(R.id.bt_send);
	}

	public void updateListMessage(String message) {
		if (message != null) {
			messageContentData.add(MessageContent
					.fromJsonToMessageContent(message));
		}
		chatAdapter.notifyDataSetChanged();
	}

	private BroadcastReceiver changeUIReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String actionUI = intent.getStringExtra(ChatService.CONTROL_ACTION);
			String message = intent.getStringExtra(ChatService.CONTROL_MESSAGE);
			KiraGameLog.d("get broadcast receive with action " + actionUI
					+ " and message " + message);
			switch (actionUI) {
//			case ChatService.CHAT_PRIVATE_ACTION_UPDATE_MESSAGES:
//				updateListMessage(message);
//				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onDestroy() {
		if (DataManager.getInstance(this).getCurrentChatUserId().equals(toUser)) {
			DataManager.getInstance(this).setCurrentChatUserId(null);
		}
		LocalBroadcastManager.getInstance(this).unregisterReceiver(
				changeUIReceiver);
		super.onDestroy();
	}
}
